package com.moter.demo01.juc.alllocks.reentrantlock;

import java.util.concurrent.TimeUnit;

/**
 * 不可重入锁
 */
public class MyUnReentrantLock {
    private volatile boolean isLocked = false;

    public synchronized void lock(){
        while(isLocked){
            try {
                wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        isLocked=true;
    }
    public synchronized void unlock(){
        isLocked=false;
        notify();
    }

    public static void main(String[] args) {
        MyUnReentrantLock lock = new MyUnReentrantLock();
        for (int i = 0; i < 10; i++) {
            new Thread(()->{
                lock.lock();
//                lock.lock();
                try {
                    TimeUnit.SECONDS.sleep(2);
                    System.out.println(Thread.currentThread().getName());
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }finally {
//                    lock.unlock();
                    lock.unlock();
                }
            }).start();
        }
    }
}
